"""
更新chapters表，添加content字段
"""
import asyncio
from tortoise import Tortoise
from tool.sessing import TORTOISE_ORM

async def update_table():
    await Tortoise.init(config=TORTOISE_ORM)
    
    # 获取数据库连接
    conn = Tortoise.get_connection("default")
    
    # SQL: 检查content字段是否存在，不存在则添加
    sql = """
    ALTER TABLE `chapters` 
    ADD COLUMN IF NOT EXISTS `content` LONGTEXT NULL COMMENT '章节内容' 
    AFTER `chapter_number`;
    """
    
    try:
        # MySQL不支持IF NOT EXISTS，我们需要手动检查
        await conn.execute_query("ALTER TABLE `chapters` ADD COLUMN `content` LONGTEXT NULL COMMENT '章节内容' AFTER `chapter_number`")
        print("[OK] 已添加content字段到chapters表")
    except Exception as e:
        if "Duplicate column name" in str(e):
            print("[INFO] content字段已存在")
        else:
            print(f"[ERROR] 添加字段失败: {e}")
    
    await Tortoise.close_connections()

asyncio.run(update_table())

